<?php
/**
 * @param number 生成随机数
 * @param string $format ALL|CHAR|NUMBER 默认所有字符和数字，包含特殊字符~!@#,
 * @return string
 */
function randstr($len = 6, $format = 'ALL') {
	switch (strtoupper ( $format )) {
		case 'ALL' :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
			break;
		case 'CHAR' :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
			break;
		case 'NUMBER' :
			$chars = '0123456789';
			break;
		default :
			$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
			break;
	}
	mt_srand ( ( double ) microtime () * 1000000 * getmypid () );
	$password = "";
	while ( strlen ( $password ) < $len )
		$password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
	return $password;
}
/**
 *
 * @see 显示订单状态
 * @param number $status        	
 * @return string
 */
function AdministrativeCategory_str($category_id) {
	$str_ = array (
			"1" => "违法通知书",
			"2" => "行政处罚立案表",
			"3" => "行政处罚案件调查笔录",
			"4" => "行政处罚决定事前告知书",
			"5" => "行政处罚陈述（申辩）书",
			"6" => "行政处罚陈述（申辩）笔录",
			"7" => "行政处罚听证告知书",
			"8" => "行政处罚听证申请书",
			"9" => "行政处罚听证通知书",
			"10" => "行政处罚听证公告",
			"11" => "行政处罚听证笔录",
			"12" => "行政处罚听证报告",
			"13" => "案件集体讨论记录",
			"14" => "行政处罚审批表",
			"15" => "案件移送函",
			"16" => "行政处罚决定书",
			"17" => "行政处罚决定书送达回证",
			"18" => "强制执行审批表",
			"19" => "强制执行通知书",
			"20" => "强制执行申请书",
			"21" => "行政处罚案件结案报告" 
	);
	return $str_ [$category_id];
}

/**
 *
 * @see 计算两个时间间隔
 *
 * @param DateTime $start_time
 *        	开始时间
 * @param DateTime $end_time
 *        	结束时间
 * @param string $format
 *        	D|d天，H|h小时，M|m分钟,默认天
 */
function getdatediff($start_time, $end_time, $format = NULL) {
	// fb ( $start_time );
	// fb ( $end_time );
	if (gettype ( $start_time ) === "integer") {
		
		$start_time = date ( 'Y-m-d H:i:s', $start_time );
	}
	if (gettype ( $end_time ) === "integer") {
		$end_time = date ( 'Y-m-d H:i:s', $end_time );
	}
	$one = strtotime ( $start_time ); // 开始时间 时间戳
	$tow = strtotime ( $end_time ); // 结束时间 时间戳
	$cle = $tow - $one;
	$cle = bindec ( decbin ( $cle ) );
	// TODO 算法益处
	// fb ( bindec ( decbin ( $cle ) ) );
	// fb ( $cle );
	$result = 0;
	switch (strtolower ( $format )) {
		default :
			$result = ceil ( $cle / 3600 / 24 );
			break;
		case 'd' :
			$result = ceil ( $cle / 3600 / 24 );
			break;
		case 'm' :
			$result = ceil ( $cle / 60 );
			break;
		case 'h' :
			$result = ceil ( $cle / 3600 );
			break;
	}
	return $result;
}

/**
 * unicode解密
 *
 * @param unknown $str        	
 * @return string|unknown
 */
function unescape($str) {
	$ret = '';
	$len = strlen ( $str );
	for($i = 0; $i < $len; $i ++) {
		if ($str [$i] == '%' && $str [$i + 1] == 'u') {
			$val = hexdec ( substr ( $str, $i + 2, 4 ) );
			if ($val < 0x7f)
				$ret .= chr ( $val );
			else if ($val < 0x800)
				$ret .= chr ( 0xc0 | ($val >> 6) ) . chr ( 0x80 | ($val & 0x3f) );
			else
				$ret .= chr ( 0xe0 | ($val >> 12) ) . chr ( 0x80 | (($val >> 6) & 0x3f) ) . chr ( 0x80 | ($val & 0x3f) );
			$i += 5;
		} else if ($str [$i] == '%') {
			$ret .= urldecode ( substr ( $str, $i, 3 ) );
			$i += 2;
		} else
			$ret .= $str [$i];
	}
	return $ret;
}
/**
 *
 * @see unicode加密
 * @param unknown $str        	
 * @return string
 */
function escape($str) {
	preg_match_all ( "/[\x80-\xff].|[\x01-\x7f]+/", $str, $newstr );
	$ar = $newstr [0];
	foreach ( $ar as $k => $v ) {
		if (ord ( $ar [$k] ) >= 127) {
			$tmpString = bin2hex ( iconv ( "utf-8", "ucs-2", $v ) );
			if (! eregi ( "WIN", PHP_OS )) {
				$tmpString = substr ( $tmpString, 2, 2 ) . substr ( $tmpString, 0, 2 );
			}
			$reString .= "%u" . $tmpString;
		} else {
			$reString .= rawurlencode ( $v );
		}
	}
	return $reString;
}

/**
 *
 * @see 截取字符串
 * @param unknown $string        	
 * @param unknown $length        	
 * @param string $encoding        	
 * @return string
 */
function getstr($string, $length, $encoding = 'utf-8') {
	$string = trim ( $string );
	if ($length && strlen ( $string ) > $length) {
		// 截断字符
		$wordscut = '';
		if (strtolower ( $encoding ) == 'utf-8') {
			// utf8编码
			$n = 0;
			$tn = 0;
			$noc = 0;
			while ( $n < strlen ( $string ) ) {
				$t = ord ( $string [$n] );
				if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1;
					$n ++;
					$noc ++;
				} elseif (194 <= $t && $t <= 223) {
					$tn = 2;
					$n += 2;
					$noc += 2;
				} elseif (224 <= $t && $t < 239) {
					$tn = 3;
					$n += 3;
					$noc += 2;
				} elseif (240 <= $t && $t <= 247) {
					$tn = 4;
					$n += 4;
					$noc += 2;
				} elseif (248 <= $t && $t <= 251) {
					$tn = 5;
					$n += 5;
					$noc += 2;
				} elseif ($t == 252 || $t == 253) {
					$tn = 6;
					$n += 6;
					$noc += 2;
				} else {
					$n ++;
				}
				if ($noc >= $length) {
					break;
				}
			}
			if ($noc > $length) {
				$n -= $tn;
			}
			$wordscut = substr ( $string, 0, $n );
		} else {
			for($i = 0; $i < $length - 1; $i ++) {
				if (ord ( $string [$i] ) > 127) {
					$wordscut .= $string [$i] . $string [$i + 1];
					$i ++;
				} else {
					$wordscut .= $string [$i];
				}
			}
		}
		$string = $wordscut;
	}
	return trim ( $string );
}
/**
 *
 * @see 显示时间
 * @param time $time
 *        	时间格式字符串
 * @return string
 */
function showtime($time) {
	if (getdatediff ( $time, time (), 'm' ) < 60) {
		return getdatediff ( $time, time (), 'm' ) . "分钟前";
	} else if (getdatediff ( $time, time (), 'h' ) < 24) {
		return getdatediff ( $time, time (), 'h' ) . "小时前";
	} else {
		return date ( "Y-m-d", strtotime ( $time ) );
	}
}

// 说明：获取完整URL
function curPageURL() {
	$pageURL = 'http';
	
	if ($_SERVER ["HTTPS"] == "on") {
		$pageURL .= "s";
	}
	$pageURL .= "://";
	
	if ($_SERVER ["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER ["SERVER_NAME"] . ":" . $_SERVER ["SERVER_PORT"] . $_SERVER ["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER ["SERVER_NAME"] . $_SERVER ["REQUEST_URI"];
	}
	return $pageURL;
}
/**
 * 校验日期格式是否正确
 *
 * @param string $date
 *        	日期
 * @param string $formats
 *        	需要检验的格式数组
 * @return boolean
 */
function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d")) {
	$unixTime = strtotime ( $date );
	if (! $unixTime) { // strtotime转换不对，日期格式显然不对。
		return false;
	}
	// 校验日期的有效性，只要满足其中一个格式就OK
	foreach ( $formats as $format ) {
		if (date ( $format, $unixTime ) == $date) {
			return true;
		}
	}
	return false;
}
/**
 *
 * @see 二维数组搜索
 * @param mixed $mixed
 *        	需要搜索的数组
 * @param string $key
 *        	搜索数组键名称
 * @param string $value
 *        	搜索数组键值
 * @param mixed $mixed_key
 *        	要获取数组中键对应的值,为空时返回存储对象
 *        	
 * @return mixed if exists or return false
 */
function array_seach_multi($mixed, $key, $value, $mixed_key = NULL) {
	if (is_array ( $mixed ) && count ( $mixed ) > 0) {
		$_exists = false;
		$ret;
		foreach ( $mixed as $item ) {
			if ($item [$key] == $value) {
				$_exists = true;
				$ret = $item;
				break;
			}
		}
		if ($_exists === true) {
			if ($mixed_key != null || $mixed_key != "")
				return $ret [$mixed_key];
			else
				return $ret;
		} else
			return "";
	}
	return "";
}
/**
 *
 * @see 获取搜索数组对象的索引
 * @param mixed $mixed        	
 * @param string $key        	
 * @param string $value        	
 */
function array_serach_multi_index($mixed, $key, $value) {
	if (is_array ( $mixed ) && count ( $mixed ) > 0) {
		$_exists = false;
		$ret;
		$index = 0;
		foreach ( $mixed as $item ) {
			if ($item [$key] == $value) {
				$_exists = true;
				$ret = $index;
				break;
			}
			$index ++;
		}
		if ($_exists === true) {
			return $ret;
		} else
			return - 1;
	}
	return - 1;
}
/**
 *
 * @see 二维数组删除指定的数组元素
 * @param array $mixed
 *        	要操作的数组
 * @param string $key
 *        	二维数组搜索键
 * @param string $value        	
 */
function array_splice_multi($mixed, $key, $value) {
	$result = array_serach_multi_index ( $mixed, $key, $value );
	array_splice ( $mixed, $result, 1 );
	return $mixed;
}
function sys_debug_log($message) {
	if (is_array ( $message ))
		fb ( $message );
	else
		fb ( date ( 'Y-m-d H:i:s', time () ) . "=>" . $message );
}
/**
 *
 * @see 获取途径对应性别选择显示
 * @param unknown $typ        	
 * @return Ambigous <string>
 */
function sex_msg($typ) {
	$types = array (
			'0' => '请选择',
			"1" => '男',
			'2' => '女' 
	);
	return $types [$typ];
}